package team.scau.laboratory_repair.web.sys;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.common.json.JqGridResult;
import team.scau.laboratory_repair.modules.po.Perm;
import team.scau.laboratory_repair.service.PermService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 30254
 * creadtedate:2018/8/19
 */
@Controller
@CommonsLog
@RequestMapping("/pc/sys/perm")
public class PermController extends BaseController {

    @Autowired
    PermService permService;

    @ModelAttribute("perm")
    public Perm get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return permService.findById(id);
        } else {
            Perm perm = new Perm();
            perm.setId(0L);
            return perm;
        }
    }

    @RequiresPermissions("sys:perm:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        return "/pc/sys/perm/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:perm:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public JqGridResult list(@RequestParam(name = "pid",defaultValue = "0") Long pid) throws Exception{
        List<Perm> perms = permService.selectSubPerms(pid);
        JqGridResult result = new JqGridResult(perms);
        return result;
    }

    @RequiresPermissions("sys:perm:edit")
    @GetMapping(value = "/add", produces = "text/html;charset=utf-8")
    public String add(@ModelAttribute("perm") Perm perm, Model model) throws Exception{
        perm.setPid(perm.getId());
        perm.setId(null);
        perm.setName(null);
        model.addAttribute("perm",perm);
        return "/pc/sys/perm/edit";
    }

    @RequiresPermissions("sys:perm:edit")
    @GetMapping(value = "/edit", produces = "text/html;charset=utf-8")
    public String edit(@ModelAttribute("perm") Perm perm, Model model) throws Exception{
        model.addAttribute("perm",perm);
        return "/pc/sys/perm/edit";
    }

    /**
     * 更新权限／菜单信息
     * @param perm
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:perm:edit")
    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Perm perm){
        Map<String,Object> result = new HashMap<>();
        try {
            permService.save(perm);
            result.put("status",200);
            result.put("msg","权限／菜单信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","权限／菜单信息保存失败");
        }
        return result;
    }

    /**
     * 删除权限／菜单
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:perm:delete")
    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap<>();
        try {
            if(id != null && id > 0){
                permService.deletePerm(id);
                result.put("status",200);
                result.put("msg","删除权限／菜单成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的权限／菜单");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","删除权限／菜单失败");
        }
        return result;
    }
}
